<template>
  <div class="pie" ref="chartRef"></div>
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
export default {
  name: 'PieChartComponent',
  data () {
    return {
      option: {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'horizontal',
          bottom: '0'
        },
        series: [
          {
            name: '分类数据',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 1048, name: '分类1', itemStyle: { color: '#53a8ff' } },
              { value: 735, name: '分类2', itemStyle: { color: '#66b1ff' } },
              { value: 580, name: '分类3', itemStyle: { color: '#79bbff' } }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      const chartDom = this.$refs.chartRef
      const myChart = echarts.init(chartDom)
      this.$watch('categoryStatistics', (newValue) => {
      // 更新 series.data 为 categoryStatistics
        this.option.series[0].data = newValue.map(item => ({
          value: item.value,
          name: item.name,
          itemStyle: item.itemStyle
        }))
        myChart.setOption(this.option)
      }, { immediate: true })
    })
  },
  created () {
    this.$store.dispatch('Book/getCategoryStatistics')
  },
  computed: {
    ...mapState('Book', ['categoryStatistics'])
  }
}
</script>

<style>
  .pie{
    width: 600px;
    height: 250px;
  }
</style>
